package cn.engage.product.controller;

import cn.engage.basic.util.LoginContext;
import cn.engage.org.domain.Employee;
import cn.engage.pet.dto.AcceptDto;
import cn.engage.product.service.IProductCommentService;
import cn.engage.product.domain.ProductComment;
import cn.engage.product.query.ProductCommentQuery;
import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.util.PageList;
import cn.engage.user.domain.Logininfo;
import cn.engage.user.domain.User;
import cn.engage.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/productComment")
public class ProductCommentController {
    @Autowired
    public IProductCommentService productCommentService;
    @Autowired
    public UserMapper userMapper;

    /**
     * 保存和修改公用的
     * @param productComment  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductComment productComment){
        try {
            if( productComment.getId()!=null)
                productCommentService.update(productComment);
            else
                productCommentService.add(productComment);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productCommentService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public ProductComment get(@PathVariable("id")Long id) {
        return productCommentService.findByIdOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<ProductComment> list(){
        return productCommentService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<ProductComment> json(@RequestBody ProductCommentQuery query) {
        return productCommentService.queryByPage(query);
    }

    // 接收订单
    @GetMapping("/order/{id}")
    public AjaxResult order(@PathVariable("id")Long id, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userMapper.findBylogininfoId(logininfo.getId());
        ProductComment productComment = new ProductComment();
        productComment.setUerId(user.getId());
        productCommentService.order(id, productComment);
        return AjaxResult.me().success();
    }

    /* 接收订单 */
    @PostMapping("/getOrder")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto) {
        productCommentService.accept(acceptDto);
        return AjaxResult.me().success();
    }
    /* 拒单 */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId, HttpServletRequest request) {
        productCommentService.reject(msgId);
        return AjaxResult.me().success();
    }


}
